#include "game.h"

Game::Game(size_t width, size_t height)
	:mFrame(60), mWindow(sf::VideoMode(width, height), "Game")
	, mWorld(mWindow)
{
	
}

void Game::run()
{
	sf::Clock clock;
	sf::Time dt = sf::Time::Zero;
	while (mWindow.isOpen()) {
		processInput();
		dt += clock.restart();
		if (dt.asSeconds() < 1.f / mFrame) {
			continue;
		}
		update(dt);
		draw();
		dt -= sf::seconds(1.f / mFrame);
	}
}

void Game::update(sf::Time dt)
{
	mWorld.update(dt);
}

void Game::draw()
{
	mWindow.clear(sf::Color::Black);
	mWorld.draw();
	mWindow.display();
}

void Game::processInput()
{
	CommandQueue& commands = mWorld.getCommandQueue();
	
	sf::Event event;
	while (mWindow.pollEvent(event)) {
		mPlayer.handleEvent(event, commands);

		if (event.type == sf::Event::Closed)
			mWindow.close();
	}

	mPlayer.handleRealtimeInput(commands);
}


